6 #define EPSILON 0.000001
12 bool operator < (const Point
&p
) const {
13 return (x
< p
.x
|| (x
== p
.x
&& y
< p
.y
));
16 double dist(const Point
&p
) const {
17 return hypot(p
.x
-x
, p
.y
-y
);
21 cout
<< "("<<x
<<","<<y
<<")\n";
29 void makePointSlopeLine(Point p
, double m
){
32 c
= -((a
*p
.x
) + (b
*p
.y
));
34 bool parallel(const Line
&l
) const {
35 return ((fabs(a
- l
.a
) <= EPSILON
) && (fabs(b
- l
.b
) <= EPSILON
));
38 bool equals (const Line
&l
) const {
39 return (parallel(l
) && (fabs(c
-l
.c
) <= EPSILON
));
42 Point
intersection(const Line
&l
) const {
43 assert( !(equals(l
)) );
44 assert( !(parallel(l
)));
46 r
.x
= (l
.b
*c
- b
*l
.c
) / (l
.a
*b
- a
*l
.b
);
47 if (fabs(b
) > EPSILON
){
48 r
.y
= - (a
* r
.x
+ c
) / b
;
50 r
.y
= - (l
.a
* r
.x
+ l
.c
) / l
.b
;
58 for (int i
=0; i
<3; ++i
){
64 while (p
[0].x
!= 0 || p
[0].y
!= 0 || p
[1].x
!= 0 || p
[1].y
!= 0 || p
[2].x
!= 0 || p
[2].y
!= 0){
68 // for (int i=0; i<3; ++i){
74 pm1
.x
= (p
[0].x
+ p
[1].x
) / 2.0;
75 pm1
.y
= (p
[0].y
+ p
[1].y
) / 2.0;
77 pm2
.x
= (p
[1].x
+ p
[2].x
) / 2.0;
78 pm2
.y
= (p
[1].y
+ p
[2].y
) / 2.0;
82 m1
= (p
[0].y
- p
[1].y
)/(p
[0].x
- p
[1].x
);
83 m2
= (p
[1].y
- p
[2].y
)/(p
[1].x
- p
[2].x
);
87 //cout << pmm1<<" "<<pmm2<<endl;
90 s1
.makePointSlopeLine(pm1
, pmm1
);
91 s2
.makePointSlopeLine(pm2
, pmm2
);
93 //if (s1.parallel(s2)){
94 if (fabs(p
[0].x
*(p
[1].y
- p
[2].y
) +
95 p
[1].x
*(p
[2].y
- p
[0].y
) +
96 p
[2].x
*(p
[0].y
- p
[1].y
)) < EPSILON
){
97 cout
<< "Andy's big heart will break.\n";
99 Point intersection
= s1
.intersection(s2
);
100 assert(intersection
.dist(p
[0]) - intersection
.dist(p
[1]) <= EPSILON
);
101 assert(intersection
.dist(p
[1]) - intersection
.dist(p
[2]) <= EPSILON
);
102 assert(intersection
.dist(p
[2]) - intersection
.dist(p
[0]) <= EPSILON
);
103 //cout << intersection.dist(p[0])<<endl;
104 //cout << intersection.dist(p[1])<<endl;
105 //cout << intersection.dist(p[2])<<endl;
107 printf("Andy should move to the house at point (%.3f, %.3f).\n", intersection
.x
, intersection
.y
);
114 for (int i
=0; i
<3; ++i
){